package com.lixinxinlove.jaychoumusic.service

import android.content.Intent
import android.net.Uri
import android.os.Binder
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.util.Pair
import androidx.lifecycle.LifecycleService
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.ConcatenatingMediaSource
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.trackselection.ExoTrackSelection
import com.google.android.exoplayer2.trackselection.MappingTrackSelector
import com.google.android.exoplayer2.ui.StyledPlayerControlView
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.EventLogger
import com.lixinxinlove.jaychoumusic._interface.IMusicPlay
import com.lixinxinlove.jaychoumusic.util.DateTimeUtil

class MusicPlayService : LifecycleService() {

    private val url = "http://82.156.11.23/mp3/爱在西元前.mp3"

    private lateinit var player: SimpleExoPlayer

    override fun onBind(intent: Intent): IBinder {
        super.onBind(intent)
        return MyBinder()
    }

    inner class MyBinder : Binder(), IMusicPlay {
        override fun play() {
            startMp3()
        }

        override fun stop() {
            if (player != null) {
                player.stop()
            }
        }

        override fun start() {

        }

        override fun next() {
        }

        override fun previous() {
        }
    }

    override fun onCreate() {
        super.onCreate()
        player = SimpleExoPlayer.Builder(this).build()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        player.release()
    }


    private fun startMp3() {
        //   val playView = StyledPlayerControlView(this)
        //  playView.player = player

        //  创建mediaItem
        val mediaItem = MediaItem.fromUri(url)
        // 设置mediaItem
        player.setMediaItem(mediaItem)
        // 添加一个监听器来接收来自播放器的事件.
        player.addListener(PlayerListener())
        // player.addAnalyticsListener(EventLogger(LeeMappingTrackSelector()))
        //  准备播放
        player.prepare()
        // 开始播放
        player.play()


//        Handler(Looper.getMainLooper()).postDelayed({
//            player.seekTo(90000)
//        },2000)



    }


    private inner class PlayerListener : Player.Listener {

        override fun onIsPlayingChanged(isPlaying: Boolean) {
            super.onIsPlayingChanged(isPlaying)

            Log.e("MusicPlayService", "onIsPlayingChanged=${isPlaying}")


        }


        override fun onPlayerError(error: ExoPlaybackException) {
            super.onPlayerError(error)
            Log.e("MusicPlayService", "onPlayerError=${error.toString()}")
        }


        override fun onEvents(player: Player, events: Player.Events) {
            super.onEvents(player, events)
            Log.e("MusicPlayService", "onEvents=${events.size()}")
           // Log.e("MusicPlayService", "音频的时长=${player.contentDuration}")
           // Log.e("MusicPlayService", "音频的时长=${player.duration}")
            Log.e("MusicPlayService", "player.currentLiveOffset=${player.currentLiveOffset}")


            if (player.contentDuration>0){
                Log.e("MusicPlayService", "player=${DateTimeUtil.secondToTime(player.duration/1000)}")

            }

        }
    }

}